package org.batatasframework.chart.columns;

import java.math.BigDecimal;
import java.text.DecimalFormat;

import org.batatasframework.chart.graphics.ChartGraphic;

public class ColumnValueNumber extends AbstractFormatableColumn<Number> implements ChartColumnValue{

	private final int indexOf;

//	private int ordinal;
	private String unit;
//	
//	@Override
//	public ChartColumnValue initialize(OrdinalAllocatable allocator) {
//		this.ordinal = allocator.allocate();
//		return this;
//	}
	
	public ColumnValueNumber(String name, int indexOf) {
		super(name);
		this.indexOf = indexOf;
	}

	@Override
	public ChartColumnValue setUnit(String unit) {
		this.unit = unit;
		return this;
	}
	
	@Override
	public String toString() {
		String value = super.toString();
		if(null != unit) value += "(" + unit + ")";
		return value;
	}
	
	public ColumnValueNumber(String name, int indexOf, String pattern) {
		super(name);
		this.indexOf = indexOf;
		this.format = new DecimalFormat(pattern);
	}

	@Override
	protected Number internalParseValue(Object[] row) {
		return(indexOf < row.length) ? (Number)row[indexOf] : null;
	}
	
	@Override
	protected Number mergedValue(Number oldValue, Number newValue) {
		if(newValue instanceof Double || newValue instanceof BigDecimal || newValue instanceof Float){
			return oldValue.doubleValue() + newValue.doubleValue();
		}
		return oldValue.longValue() + newValue.longValue();
	}
	
	@Override
	public ChartColumnValue graphic(ChartGraphic... graphics) {
		for(ChartGraphic graphic : graphics){
			graphic.add(this);
		}
		return this;
	}
	
//	@Override
//	public int getOrdinal() {
//		return ordinal;
//	}
}
